package com.lemon.secondhandplatform.ui.activity.comment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.lemon.secondhandplatform.BaseApplication;
import com.lemon.secondhandplatform.R;
import com.lemon.secondhandplatform.mode.Comment;
import com.lemon.secondhandplatform.mode.Good;
import com.lemon.secondhandplatform.service.ShpService;
import com.lemon.secondhandplatform.ui.activity.BaseActivity;
import com.lemon.secondhandplatform.ui.adapter.CommentAdapter;
import com.lemon.secondhandplatform.ui.util.StatusBarUtils;
import com.lemon.secondhandplatform.ui.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

public class CommentActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.edtComment)
    EditText edtComment;
    //发送按钮
    @BindView(R.id.btnSendComment)
    Button btnSendComment;

    private Good good;
    private List<Comment> comments;
    private CommentAdapter adapter;

    //数据
    public static final String EXTRA_DATA = "CommentActivity.EXTRA_DATA";

    public static Intent newIntent(Context context, Good data) {
        Intent intent = new Intent(context, CommentActivity.class);
        intent.putExtra(EXTRA_DATA, data);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comments);
        bindService();

        initView();
        bindListener();
    }

    public void initView() {
        StatusBarUtils.drawableStatus(this, R.color.colorPrimary);
        good = (Good) getIntent().getSerializableExtra(EXTRA_DATA);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CommentAdapter(this, comments);
        recyclerView.setAdapter(adapter);
    }

    public void bindListener() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 发送
     *
     * @param v
     */
    @OnClick(R.id.btnSendComment)
    public void send(View v) {
        final Comment comment = new Comment();
        comment.setOwner(BaseApplication.getUser());
        comment.setContent(edtComment.getText().toString());
        comment.setGood(good);
        comment.setFloor(comments == null ? 1 : comments.size() + 1);

        if (comments == null) {
            comments = new ArrayList<>();
        }

        comment.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null) {
                    ToastUtils.show("评论成功");
                    comments.add(comment);
                    adapter.refresh(comments);
                    edtComment.setText("");
                }
            }
        });

    }

    @Override
    protected void afterBindService(ShpService.ShpBinder binder) {
        super.afterBindService(binder);
        binder.listComments(good, comments == null ? 0 : comments.size(), new FindListener<Comment>() {
            @Override
            public void done(List<Comment> list, BmobException e) {
                if (e == null) {
                    comments = list;
                    adapter.refresh(list);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        updateUI();
    }

    private void updateUI() {
        if (binder != null) {
            binder.listComments(good, comments == null ? 0 : comments.size(), new FindListener<Comment>() {
                @Override
                public void done(List<Comment> list, BmobException e) {
                    if (e == null) {
                        adapter.append(list);
                    }
                }
            });
        }
    }
}
